/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package cn.edu.thu.log.xes;

import java.util.Date;
import java.util.Hashtable;

import org.deckfour.xes.model.XTrace;

import cn.edu.thu.log.read.LogBufferModified;
import cn.edu.thu.log.util.Format;
import cn.edu.thu.log.util.PanGuConstants;
/**
 * 
 * @author Student
 *
 */
public class XESConvertorMonitor {

	private static XESConvertorMonitor singleton = null;
/**
 * 
 * @return
 */
	public static synchronized XESConvertorMonitor instance() {
		if (singleton == null) {
			singleton = new XESConvertorMonitor();
		}
		return singleton;
	}

	private int numOfEvent;
	public int getNumOfEvent() {
		return numOfEvent;
	}
	public void setNumOfEvent(int numOfEvent) {
		this.numOfEvent = numOfEvent;
	}

	/**
	 * map of laster arrival time in one case
	 */
	private Hashtable<XTrace, String> lastestArrivalMap;

	/**
	 * map of earliest arrival time . one case
	 */
	// private Hashtable<XTrace, String> earliestArrivalMap;
	public XESConvertorMonitor() {
		this.numOfEvent = 0;
		this.lastestArrivalMap = new Hashtable<XTrace, String>();
	}
/**
 * 
 * @return
 */
	public boolean ifExceedMaxEventNum() {
		if (numOfEvent > PanGuConstants.MAX_EVENT_NUM) {
			return true;
		}
		return false;
	}
/**
 * 
 * @param addSize parameter
 */
	public void addEventNum(int addSize) {
		numOfEvent = numOfEvent + addSize;
	}

	/**
	 * check if the logBuffer arriveTime is within the time limit of one case
	 * 
	 * @param logBuffer arrived logBuffer
	 * @param trace the case's caseID
	 * @return true:logBuffer attive within case; false: logBuffer timeout
	 */
	public boolean ifTimeOut(LogBufferModified logBuffer, XTrace trace) {
		boolean ifTimeOut = false;
		String lasterTimeString = getLastestArrivalMap().get(trace);
		String arriveTimeString = logBuffer.getTimeStamp();
		Date lasterTime = Format.stringToTimeStamp(lasterTimeString);
		Date arriveTime = Format.stringToTimeStamp(arriveTimeString);
		if (arriveTime.after(lasterTime)) {
			if (Math.abs(arriveTime.getTime() - lasterTime.getTime()) > PanGuConstants.TIMEROUT) {
				ifTimeOut = true;
			}
		}
		return ifTimeOut;
	}

	/**
	 * check if the logBuffer arriveTime is within the time limit of one case
	 * 
	 * @param preTimestamp arrived logBuffer
	 * @param currentTimestamp the case's caseID
	 * @return true:logBuffer attive within case; false: logBuffer timeout
	 */
	public boolean ifTimeOut(String preTimestamp, String currentTimestamp) {
		// Time time=new Time();
		long timeOut = PanGuConstants.TIMEROUT;
		// System.out.print("\ncheck if timeout");
		boolean ifTimeOut = false;
		// if(!getLastestArrivalMap().contains(trace)){//already remove this
		// trace when write complete(timed out) trace to logReplace
		// ifTimeOut=true;
		// return ifTimeOut;
		// }
		String lasterTimeString = preTimestamp;
		// String earliestTimeString = getEarliestArrivalMap().get(trace);
		String arriveTimeString = currentTimestamp;
		Date lasterTime = Format.stringToTimeStamp(lasterTimeString);
		// Date earliestTime = Format.StringToTimeStamp(earliestTimeString);
		Date arriveTime = Format.stringToTimeStamp(arriveTimeString);
		if (arriveTime.after(lasterTime)) {
			if (Math.abs(arriveTime.getTime() - lasterTime.getTime()) > timeOut) {
				ifTimeOut = true;

			}
		}

		return ifTimeOut;
	}
/**
 * 
 * @param trace parameter
 * @param timeStamp parameter
 */
	public void updateLastestArrivalMap(XTrace trace, String timeStamp) {
		getLastestArrivalMap().put(trace, timeStamp);
	}

	/**
	 * @return the lastestArrivalMap
	 */
	public Hashtable<XTrace, String> getLastestArrivalMap() {
		return lastestArrivalMap;
	}

	/**
	 * @param lastestArrivalMap
	 *            the lastestArrivalMap to set
	 */
	public void setLastestArrivalMap(Hashtable<XTrace, String> lastestArrivalMap) {
		this.lastestArrivalMap = lastestArrivalMap;
	}

}
